package org.buptsse.listview;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.buptsse.entity.BookItem;
import org.buptsse.entity.CLHotPost;
import org.buptsse.entity.Circle;
import org.buptsse.entity.HistoryItem;
import org.buptsse.entity.User;
import org.buptsse.ui.LogoActivity;
import org.buptsse.ui.OneTradeActivity;
import org.buptsse.ui.R;
//import org.buptsse.ui.TreeActivity;
import org.buptsse.utils.ActionUtils;
import org.buptsse.utils.GloableInstance;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class TradeUndonePage extends LinearLayout implements OnScrollListener {
	private final static int OK = 1;
	private final String URL_CIRCLELIST="fetchJoinCircle.do";//——确定调用函数
	ListView list;//——信件一个list
	int scrollState;
	int count = 40;
	int lastItem;
	int visibleItemCount;
	//Button footerButton;
	//LinearLayout footerProgressBarLayout;// 加载进度滚动条
	ListAdapter listAdapter = new ListAdapter();
	//View viewfoot;
	private User user=null;//——一个用户
	private int uid;
	private List<HistoryItem> UndoneList = new LinkedList<HistoryItem>();//——新建一个叫circle类型的数组
	private Context context;
	private ListView listView = null;//——一个界面显示
	
	private TextView bookNameTV,otherNameTV,actionTV,beginTimeTV,priceTV;
	
	
	//——这个是函数
	Handler myHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case TradeUndonePage.OK:
				listAdapter.notifyDataSetChanged();
				break;
			}
			super.handleMessage(msg);
		}
	};

	public TradeUndonePage(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public TradeUndonePage(Context context, User user) {
		super(context);
		this.user = user;
		this.context = context;
		uid=124;
		init();
	}

	private void init() {
		setBackgroundColor(Color.parseColor("#ecf6eb"));
		listAdapter.count=0;
		listView = new ListView(getContext());
		listView.setDivider(new ColorDrawable(Color.parseColor("#ecf6eb")));
		listView.setDividerHeight(1);
		listView.setVerticalFadingEdgeEnabled(false);
		listView.setAdapter(listAdapter);
		listView.setOnScrollListener(this);
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				HistoryItem item=UndoneList.get(arg2);
				Bundle bundle=new Bundle();
				bundle.putString("dealid", item.getDealid());
				if(item.getSeller().equals(GloableInstance.user.getName()))
				{
					bundle.putString("role", "卖家");
				}else {
					bundle.putString("role", "买家");
				}
				Intent intent=new Intent();
				intent.setClass(context, OneTradeActivity.class);
				intent.putExtras(bundle);
				context.startActivity(intent);
			}
		});
		LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT);
		addView(listView, params);
	}
	
	public void FillItems(LinkedList<HistoryItem> list)
	{
		UndoneList=list;
		listAdapter.count=list.size();
		listAdapter.notifyDataSetChanged();
	}
	
	class ListAdapter extends BaseAdapter {

		int count = 10;

		@Override
		public int getCount() {
			return count;
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = LayoutInflater.from(getContext());
			View view = inflater.inflate(R.layout.listview_item_trade, null);
			
			bookNameTV=(TextView) view.findViewById(R.id.bookname);
			otherNameTV=(TextView) view.findViewById(R.id.other);
			actionTV=(TextView) view.findViewById(R.id.type);
			beginTimeTV=(TextView) view.findViewById(R.id.begintime);
			priceTV=(TextView) view.findViewById(R.id.price);
			
			HistoryItem item=UndoneList.get(position);
			
			bookNameTV.setText(item.getBookname());
			if(item.getBuyer().equals(GloableInstance.user.getName()))
			{
				actionTV.setText("买入");
				otherNameTV.setText(item.getSeller());
			}else {
				actionTV.setText("卖出");
				otherNameTV.setText(item.getBuyer());
			}
			
			beginTimeTV.setText(item.getBegintime());
			priceTV.setText(item.getPrice());
			return view;
		}
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		this.visibleItemCount = visibleItemCount;
		lastItem = firstVisibleItem + visibleItemCount - 1;
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		this.scrollState = scrollState;
	}
}
